package admission.ecnu.service;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import ToolPackage.Json;
import admission.ecnu.dao.UserDAO;
import admission.ecnu.dao.classInfoDao;
import admission.ecnu.interfaces.IRnsService;
import admission.ecnu.mainservlet.RnsContext;
import admission.ecnu.object.DataManager;
import admission.ecnu.object.OClassInfo;

public class viewAllClass implements IRnsService {

	@Override
	public void executeService(RnsContext context) throws Exception {
		// TODO Auto-generated method stub
		JSONObject obj = context.getJsonRequestObject();
		if(!DataManager.USER_ID.equals(obj.get("key"))){
			JSONObject jsonReturn = new JSONObject();
			jsonReturn.put("returnCode", DataManager.RETURN_WRONGKEY);
			context.setJsonResponseObject(jsonReturn);
			return;
		}
		ArrayList<OClassInfo> classList = (ArrayList<OClassInfo>) new classInfoDao().classSelect();
		
		JSONObject json = new JSONObject();
		json.put("returnCode", DataManager.RETURN_SUCCESSFUL);
		JSONArray jsonArray = new JSONArray();
		for(int i = 0; i<classList.size(); i++){
			JSONObject objClass = new JSONObject();
			objClass.put("grade", classList.get(i).getGrade());
			objClass.put("curriculum", classList.get(i).getCurriculum());
			objClass.put("type", classList.get(i).getType());
			objClass.put("teacher", classList.get(i).getTeacher());
			objClass.put("classTime", classList.get(i).getClassTime());
			objClass.put("classroom", classList.get(i).getClassroom());
			objClass.put("studentNum", classList.get(i).getStudentNum());
			objClass.put("isExam", classList.get(i).getIsExam());
			objClass.put("classId", classList.get(i).getClassId());
			jsonArray.put(objClass);
		}
		json.put("returnValue", jsonArray);
		context.setJsonResponseObject(json);
	}
}